package com.cn.controller.admin;

import com.cn.enums.ErrorMsg;
import com.cn.entity.User;
import com.cn.service.AdminService;
import com.cn.service.UserService;
import com.cn.vo.Result;
import com.cn.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 管理员用户管理控制器
 * 处理用户列表、更新状态、查询等功能
 */
@RestController
@RequestMapping("admin/user")
public class AdminUserController {

    @Autowired
    private UserService userService;
    
    @Autowired
    private AdminService adminService;

    @GetMapping("list")
    public Result userList(HttpSession session,
                           @RequestParam(value = "page",required = false) Integer page,
                           @RequestParam(value = "nums",required = false) Integer nums,
                           @RequestParam("status") Integer status){
        // 参数校验
        if (status == null) {
            return Result.fail(ErrorMsg.PARAM_ERROR);
        }
        
        if(session.getAttribute("admin")==null){
            return Result.fail(ErrorMsg.COOKIE_ERROR);
        }
        int p=1;
        int n=8;
        if(null!=page){
            p=page>0?page:1;
        }
        if(null!=nums){
            n=nums>0?nums:8;
        }
        PageResult<User> pageResult = userService.getUserByStatus(status,p,n);
        return Result.success(pageResult);
    }

    @GetMapping("updateStatus")
    public Result updateUserStatus(HttpSession session,
                                   @RequestParam("id") Long id,
                                   @RequestParam("status") Integer status){
        // 参数校验
        if (id == null) {
            return Result.fail(ErrorMsg.PARAM_ERROR);
        }
        if (status == null) {
            return Result.fail(ErrorMsg.PARAM_ERROR);
        }
        
        if(session.getAttribute("admin")==null){
            return Result.fail(ErrorMsg.COOKIE_ERROR);
        }
        User user =new User();
        user.setId(id);
        user.setUserStatus(status.byteValue());
        if(userService.updateUserInfo(user))
            return Result.success();
        return Result.fail(ErrorMsg.SYSTEM_ERROR);
    }

    // 根据用户账号来查找信息
    @GetMapping("query")
    public Result queryUser(HttpSession session,
                            @RequestParam(value = "searchValue",required = false) String searchValue,
                            @RequestParam(value = "mode",required = false) Integer mode,
                            @RequestParam(value = "page",required = false) Integer page,
                            @RequestParam(value = "nums",required = false) Integer nums){
        if(session.getAttribute("admin")==null){
            return Result.fail(ErrorMsg.COOKIE_ERROR);
        }
        int p=1;
        int n=8;
        if(null!=page){
            p=page>0?page:1;
        }
        if(null!=nums){
            n=nums>0?nums:8;
        }

        if(mode == 1){
            if(null == searchValue || "".equals(searchValue)){
                PageResult<User> pageResult = userService.getUserByStatus(0,p,n);
                return Result.success(pageResult);
            }else{
                PageResult<User> pageResult = userService.getUserByNumber(searchValue,p,n);
                return Result.success(pageResult);
            }
        }else if(mode == 2){
            if(null == searchValue || "".equals(searchValue)){
                PageResult<User> pageResult = userService.getUserByStatus(1,p,n);
                return Result.success(pageResult);
            }else{
                PageResult<User> pageResult = userService.getUserByNumber(searchValue,p,n);
                return Result.success(pageResult);
            }
        }else
            return Result.success(adminService.getAdminList(p,n));
    }
}